package cn.iocoder.yudao.module.project.service.kismaterielexwarehouserecycle;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.kismaterielexwarehouserecycle.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.kismaterielexwarehouserecycle.KisMaterielExWarehouseRecycleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.kismaterielexwarehouserecycle.KisMaterielExWarehouseRecycleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 物料出库单作废信息 Service 实现类
 *
 * @author 武汉华诚
 */
@Service
@Validated
public class KisMaterielExWarehouseRecycleServiceImpl implements KisMaterielExWarehouseRecycleService {

    @Resource
    private KisMaterielExWarehouseRecycleMapper kisMaterielExWarehouseRecycleMapper;

    @Override
    public Long createKisMaterielExWarehouseRecycle(KisMaterielExWarehouseRecycleSaveReqVO createReqVO) {
        // 插入
        KisMaterielExWarehouseRecycleDO kisMaterielExWarehouseRecycle = BeanUtils.toBean(createReqVO, KisMaterielExWarehouseRecycleDO.class);
        kisMaterielExWarehouseRecycleMapper.insert(kisMaterielExWarehouseRecycle);
        // 返回
        return kisMaterielExWarehouseRecycle.getRecycleId();
    }

    @Override
    public void updateKisMaterielExWarehouseRecycle(KisMaterielExWarehouseRecycleSaveReqVO updateReqVO) {
        // 校验存在
        validateKisMaterielExWarehouseRecycleExists(updateReqVO.getRecycleId());
        // 更新
        KisMaterielExWarehouseRecycleDO updateObj = BeanUtils.toBean(updateReqVO, KisMaterielExWarehouseRecycleDO.class);
        kisMaterielExWarehouseRecycleMapper.updateById(updateObj);
    }

    @Override
    public void deleteKisMaterielExWarehouseRecycle(Long id) {
        // 校验存在
        validateKisMaterielExWarehouseRecycleExists(id);
        // 删除
        kisMaterielExWarehouseRecycleMapper.deleteById(id);
    }

    private void validateKisMaterielExWarehouseRecycleExists(Long id) {
        if (kisMaterielExWarehouseRecycleMapper.selectById(id) == null) {
            throw exception(KIS_MATERIEL_EX_WAREHOUSE_RECYCLE_NOT_EXISTS);
        }
    }

    @Override
    public KisMaterielExWarehouseRecycleDO getKisMaterielExWarehouseRecycle(Long id) {
        return kisMaterielExWarehouseRecycleMapper.selectById(id);
    }

    @Override
    public PageResult<KisMaterielExWarehouseRecycleDO> getKisMaterielExWarehouseRecyclePage(KisMaterielExWarehouseRecyclePageReqVO pageReqVO) {
        return kisMaterielExWarehouseRecycleMapper.selectPage(pageReqVO);
    }

}